﻿using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Hasiu.Entity;
using Hasiu.Mvc.Extensions.Pager;
using Hasiu.Mvc.Infrastructure;

namespace Hasiu.Mvc.Controllers.Tools
{
    public class EditorToolsController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Ajax(int? pageIndex)
        {
            const string myfile = "~/file/";
            if (!Directory.Exists(Server.MapPath(myfile)))
                Directory.CreateDirectory(Server.MapPath(myfile));
            var list = Directory.GetFiles(Server.MapPath(myfile))
                .Select(v => new Files
                                 {
                                     FileName = Path.GetFileName(v),
                                     AllfileName = myfile.Substring(1) + Path.GetFileName(v),
                                     FileSize = (new FileInfo(v).Length/1024) + "KB",
                                     BackTime = new FileInfo(v).CreationTime.ToString("yyyy-MM-dd HH:mm:ss")
                                 }).ToList();
            return View(new PagerList<Files>(list, pageIndex ?? 1, 6));
        }

        public ActionResult Del(string id)
        {
            id.DeleteFile();
            return new RedirectOfDeleteResult("/tools/editor.aspx");
        }
    }
}